var dynamicPortet:String;
var autoLoginSet:Boolean;
var autoCount:Number;
var emailUniqueResult;

function init()
{ 
	// Content Context	
	Packages.model.Context.activeContent=this;

	// UI.init
	var basic=Packages.system.mlf.GateKeeper.scanElement(this._name, "loginsec");
	Packages.system.display.JAccordion.initialize(basic);

	// Event.init
	var evtTargetArr:Array=new Array();
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "forgetPwdSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "autoLoginSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "doLoginSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "registerSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "loginSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "doRegisterSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "cancelAutoLoginSpot"));
	Packages.system.events.StateFulButtonEvent.initialize(evtTargetArr);

	invokeInitComplete();
}

function dispose()
{
	delete autoCount;
	delete dynamicPortet;
	delete autoLoginSet;
}

function invokeInitComplete()
{
	autoCount=9;
	checkUserAccount();
	
	Packages.system.mlf.GateKeeper.addListener("onReleaseButtonEvent", this);
	Packages.system.mlf.GateKeeper.addListener("onTxtFocusOutEvent", this);
	Packages.system.mlf.GateKeeper.addListener("onTxtFocusOnEvent", this);

	dynamicPortet="loginmode";
}

function doRegisterHandler()
{
	var email_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "remail");
	var pwd_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "rpwd");
	var nickname_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "nickName");
	
	var obj:Object=new Object();
	obj.email=email_tf.text;
	obj.password=pwd_tf.text;
	obj.nickname=nickname_tf.text;
	 
	var data:Object=new Object();
	data.path=evt.target._parent;
	data.name="Login";
	data.uri="app/content/Login.xml";
	data.returnPath="back2login";
	data.container="resultmode";
	data.oper="memberRegister";

	Packages.modula.RequestCommand.request("REMOTING", "memberRegister", obj, Packages.model.vo.CommonRemotingResultView, "normal", this, data);
}

function doAutoLoginHandler()
{
	autoLoginSet=true;
	doLoginHandler();
}

function doCheckEmailUnique()
{
	var email_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "remail");

	var obj:Object=new Object();
	obj.email=email_tf.text;
	 
	var data:Object=new Object();
	data.invokeCall="checkEmailUniqueResult";
	data.saved="emailUniqueResult";

	Packages.modula.RequestCommand.request("REMOTING", "isEmailUnique", obj, null, "normal", this, data);
}

function checkEmailUniqueResult()
{
	if (!emailUniqueResult.success) 
	{
		//
		Packages.modula.MessageCenter.send(emailUniqueResult.errorMessage, 1, 0);
		var email_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "remail");
		email_tf.borderColor=0xff0000;
		email_tf.border=true;
	}
}

function doLoginHandler()
{
	var email_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "email");
	var pwd_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "pwd");

	var obj:Object=new Object();
	obj.email=email_tf.text;
	obj.password=pwd_tf.text;
	 
	var data:Object=new Object();
	data.path=evt.target._parent;
	data.name="Login";
	data.uri="app/content/Login.xml";
	data.returnPath="back2login";
	data.container="resultmode";
	data.initCall="checkLoginResult";
	data.oper="memberLogin";
	Packages.system.mlf.GateKeeper.addListener(data.initCall, this);

	Packages.modula.RequestCommand.request("REMOTING", "memberLogin", obj, Packages.model.vo.CommonRemotingResultView, "normal", this, data);
}

function checkLoginResult(evt:Object)
{
	// Packages.log("checkLoginResult: "+evt.param, "debug");
	Packages.system.mlf.GateKeeper.removeListener(evt.type, this);
	if (evt.param=="0")
	{
		//
		var email_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "email");
		if (autoLoginSet)
		{
			var pwd_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "pwd");
			saveUserAccount(email_tf.text, pwd_tf.text);
		}
		//
		Packages.model.Context.loginFlag=true;
		//
		var returnObj_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "returnObj");
		Packages.model.Context.sessionID=returnObj_tf.text;
		//
		Packages.model.UserProfile.init();
		//
		Packages.components.SceneAgent.loadCacheScene();
	}
}

function checkUserAccount()
{
	var info=mlf.gatekeeper.native.Caller.loadLSO("UserAccount");
	if (info.email&&info.pwd)
	{
		var email_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "email");
		var pwd_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "pwd");
		email_tf.text=info.email;
		pwd_tf.password=true;
		pwd_tf.text=info.pwd;

		var loginOperSec=Packages.system.mlf.GateKeeper.scanElement(this._name, "loginOperSec");
		loginOperSec._visible=false;
		var autoLoginOperSec=Packages.system.mlf.GateKeeper.scanElement(this._name, "autoLoginOperSec");
		autoLoginOperSec._visible=true;

		// start timer and doLoginHandler after finished
		startInterval();
	}
}

function startInterval()
{
	Packages.system.mlf.GateKeeper.addListener("onCountIntervalEvent", this);
	var noticeEvent:Object=new Object();
	noticeEvent.type="onCountIntervalEvent";
	noticeEvent.gap=1;
	noticeEvent.count=0;
	Packages.system.display.Timer.regNoticeEvent(noticeEvent);
}

function closeInterval()
{
	Packages.system.mlf.GateKeeper.removeListener("onCountIntervalEvent", this);
	Packages.system.display.Timer.removeNoticeEvent("onCountIntervalEvent");
	var loginOperSec=Packages.system.mlf.GateKeeper.scanElement(this._name, "loginOperSec");
	loginOperSec._visible=true;
	var autoLoginOperSec=Packages.system.mlf.GateKeeper.scanElement(this._name, "autoLoginOperSec");
	autoLoginOperSec._visible=false;
}

function onCountIntervalEvent(evt:Object)
{
	autoCount--;
	var autoLoginOperLabel=Packages.system.mlf.GateKeeper.scanElement(this._name, "autoLoginOperLabel");
	var info=autoLoginOperLabel.text;
	var infoArr=info.split(" ");
	autoLoginOperLabel.htmlText=Packages.system.utils.TextUtil.formatText("tfContent", autoCount+" "+infoArr[1]);
	if (autoCount==0) 
	{
		closeInterval();
		doLoginHandler();
	}
}

function saveUserAccount(email, pwd)
{
	var info:Array=new Array();
	info.email=email;
	info.pwd=pwd;
	mlf.gatekeeper.native.Caller.saveLSO("UserAccount", info);
}

function back2login()
{
	// Packages.system.mlf.GateKeeper.updatePortletView(this._name, "resultmode", "loginmode");
	Packages.system.mlf.GateKeeper.updateDynamicPortletView(this._name, "commonRemotingResult", "loginmode");
	dynamicPortet="loginmode";
}

function forgetPwd()
{
	var email_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "email");
	var obj:Object=new Object();
	obj.email=email_tf.text;

	var data:Object=new Object();
	data.name="Login";
	data.uri="app/content/Login.xml";
	data.returnPath="back2login";
	data.container="resultmode";
	data.oper="memberPasswordRetrieve";
	
	Packages.modula.RequestCommand.request("REMOTING", "memberPasswordRetrieve", obj, Packages.model.vo.CommonRemotingResultView, "normal", this, data);
}

function onReleaseButtonEvent(evt:Object)
{
	if (evt.param=="registerSpot")
	{
		Packages.system.mlf.GateKeeper.updatePortletView(this._name, dynamicPortet, "regmode");
		dynamicPortet="regmode";
	}
	else if (evt.param=="loginSpot")
	{
		Packages.system.mlf.GateKeeper.updatePortletView(this._name, dynamicPortet, "loginmode");
		dynamicPortet="loginmode";
	}
	else if (evt.param=="doRegisterSpot")
	{
		Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "resultmode"));
		doRegisterHandler();
	}
	else if (evt.param=="resultReturnSpot")
	{
		var path=Packages.system.mlf.GateKeeper.scanElement(this._name, "resultReturnPath");
		
		this[path.text].apply(this, null);
	}
	else if (evt.param=="doLoginSpot")
	{
		Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "resultmode"));
		doLoginHandler();
	}
	else if (evt.param=="autoLoginSpot")
	{
		Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "resultmode"));
		doAutoLoginHandler();
	}
	else if (evt.param=="cancelAutoLoginSpot")
	{
		closeInterval();
		saveUserAccount();
	}
	else if (evt.param=="forgetPwdSpot")
	{
		forgetPwd();
	}
}

function onTxtFocusOutEvent(evt:Object)
{
	// Packages.log(evt.type+": "+evt.param, "debug");
	if (evt.param._parent._name.indexOf("pwd")>-1)
	{
		var defaultPwdValue=Packages.system.mlf.GateKeeper.scanDictionary(evt.param, "PwdValue");
		var defaultConfirmPwdValue=Packages.system.mlf.GateKeeper.scanDictionary(evt.param, "ConfirmPwdValue");
		if (defaultPwdValue.indexOf(evt.param.text)>-1) evt.param.password=false;
		if (defaultConfirmPwdValue.indexOf(evt.param.text)>-1) evt.param.password=false;
	}
	else if (evt.param._parent._name.indexOf("remail")>-1)
	{
		if (evt.param!="") doCheckEmailUnique();
	}
}

function onTxtFocusOnEvent(evt:Object)
{
	// Packages.log(evt.type+": "+evt.param, "debug");
	if (evt.param._parent._name.indexOf("pwd")>-1)
	{
		evt.param.password=true;
	}
}

function handleResponseResult(rsp:Object)
{
	if (rsp.vo.instance.type=="Template")
	{
		Packages.system.mlf.GateKeeper.updateDynamicPortletView(this._name, dynamicPortet, "commonRemotingResult", rsp.vo.instance.content);
		// dynamicPortet="resultmode";
		// Packages.system.mlf.GateKeeper.loadML2byContent(rsp.vo.instance.content);
	}
	else if (rsp.vo.instance.type=="doWrapper")
	{
		Packages.system.mlf.GateKeeper.setValue(rsp.vo.instance.content, this._name);
	}

	if (rsp.component.data.saved!=null)
	{
		this[rsp.component.data.saved]=rsp.service.result;
	}

	if (rsp.component.data.invokeCall!=null)
	{
		this[rsp.component.data.invokeCall].apply(this, rsp.component.data.invokeArgs);
	}
	Packages.system.display.Progress.removeInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, rsp.component.data.container));
}